//
//  Trivia.m
//  MathMasterMain
//
//  Created by Jimmy Hahm on 10-03-27.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "Trivia.h"
//#import	"Reachability.h"
#import "SystemConfiguration/SCNetworkReachability.h";

//

@implementation Trivia
@synthesize Question;
@synthesize Option1;
@synthesize Option2;
@synthesize Option3;
@synthesize Option4;
@synthesize Answer;
@synthesize QType;
@synthesize IMGLocation;
@synthesize QID;
@synthesize Correction;
@synthesize Image;
@synthesize nextQuestionButton;
@synthesize	scoreButton;
/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
 if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
 // Custom initialization
 }
 return self;
 }
 */
-(BOOL)connectedToWeb{
	BOOL connected;
	const char *host = "www.apple.com";
	SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, host);
	SCNetworkReachabilityFlags flags;
	connected = SCNetworkReachabilityGetFlags(reachability, &flags);
	BOOL isConnected = connected && (flags & kSCNetworkFlagsReachable) && !(flags & kSCNetworkFlagsConnectionRequired);
	CFRelease(reachability);
	return isConnected;
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	
	/*QuestionXMLParser* xmlParser = [ QuestionXMLParser new ];
	 
	 NSURL* url = [ NSURL URLWithString: @"http://triviadb.mathmasters.x10hosting.com/G5QDB.xml" ];
	 
	 NSMutableArray* Q = [NSMutableArray alloc];
	 if([self connectedToWeb]){
	 Q = [xmlParser parseContentOfUrl:url];
	 }else {
	 Q = [xmlParser parseContentOfLocal:@"G5QDB.xml"];
	 }
	 
	 Questions *X = [[Q objectAtIndex:3] retain];
	 [Option1 setTitle:(NSString *)[X Option1] forState:UIControlStateNormal];
	 [Option2 setTitle:(NSString *)[X Option2] forState:UIControlStateNormal];
	 [Option3 setTitle:(NSString *)[X Option3] forState:UIControlStateNormal];
	 [Option4 setTitle:(NSString *)[X Option4] forState:UIControlStateNormal];
	 
	 NSString* imgL = [NSString stringWithFormat:@"%@%@",[[NSBundle mainBundle] resourcePath],[X IMGLocation]];
	 if ([X IMGLocation] != NULL) {
	 UIImage* img = [UIImage imageNamed:(NSString*)[X IMGLocation]];
	 [Image setImage:img];
	 }
	 Answer = [X Answer];
	 [Question setText:(NSString *)[X Question]];*/
	[self loadQuestion];
    [super viewDidLoad];
}

-(IBAction)loadQuestionPressed:(id)sender{
	for(int i=0; i<12; i++){
		if(questionAskedArray[i] == TRUE){
			allQuestionsUsed = TRUE;
		}else{
			allQuestionsUsed = FALSE;
			break;
		}
	}
	
	if(allQuestionsUsed == TRUE){
		for(int i=0; i<12; i++){
			questionAskedArray[i] = FALSE;
		}
		allQuestionsUsed = FALSE;
	}
	[Image setHidden:NO];
	nextQuestionButton.hidden = TRUE;
	[self loadQuestion];
}

-(void)loadQuestion{
	int questionNumber = (arc4random()%(12));
	while(questionAskedArray[questionNumber] == TRUE){
		questionNumber = (arc4random()%(12));
	}
	questionAskedArray[questionNumber] = TRUE;
	
	
	QuestionXMLParser* xmlParser = [ QuestionXMLParser new ];
	
	NSURL* url = [ NSURL URLWithString: @"http://triviadb.mathmasters.x10hosting.com/G5QDB.xml" ];
	
	NSMutableArray* Q = [NSMutableArray alloc];
	if([self connectedToWeb]){
		Q = [xmlParser parseContentOfLocal:@"G5QDB.xml"];//Q = [xmlParser parseContentOfUrl:url];
	}else {
		Q = [xmlParser parseContentOfLocal:@"G5QDB.xml"];
	}
	
	Questions *X = [[Q objectAtIndex:questionNumber] retain];
	[Option1 setTitle:(NSString *)[X Option1] forState:UIControlStateNormal];
	[Option2 setTitle:(NSString *)[X Option2] forState:UIControlStateNormal];
	[Option3 setTitle:(NSString *)[X Option3] forState:UIControlStateNormal];
	[Option4 setTitle:(NSString *)[X Option4] forState:UIControlStateNormal];
	
	NSString* imgL = [NSString stringWithFormat:@"%@%@",[[NSBundle mainBundle] resourcePath],[X IMGLocation]];
	if ([X IMGLocation] != NULL) {
		UIImage* img = [UIImage imageNamed:(NSString*)[X IMGLocation]];
		[Image setImage:img];
	}else{
		UIImage* img = [UIImage imageNamed:@"Blank.png"];
		[Image setImage:img];
	}
	Answer = [X Answer];
	[Question setText:(NSString *)[X Question]];
	
}
/*
 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 */
-(IBAction)optionPressed:(id)sender
{
	NSString *senderTitle = [sender currentTitle];
	if([senderTitle isEqualToString:Answer])
	{
		UIImage *image = [UIImage imageNamed: @"mark_correct.tif"];
		[Image setHidden:YES];
		[Correction setImage:image];
		score++;
		[scoreButton setTitle:[NSString stringWithFormat:@"Score: %d", score] forState:UIControlStateNormal];//@"Score: %d", score forState:UIControlStateNormal];
	} else {
		UIImage *image = [UIImage imageNamed: @"mark_wrong.tif"];
		[Image setHidden:YES];
		[Correction setImage:image];
	}
	nextQuestionButton.hidden = FALSE;
	lastScore = score;
}

- (IBAction)closeTrivia:(id)sender
{
	//lastScore = score;
	[self dismissModalViewControllerAnimated:YES];
}

-(int)returnScore{
	return lastScore;
}

- (void)didReceiveMemoryWarning {
	//[xmlParser release];
	//[url release];
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
